package com.dario.client;

import java.util.List;

import com.dario.IbatisTest.Contact;
import com.dario.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class GWTTest implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	private SetGrid gridTable;
	
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	private final GetDataServiceAsync getDataService = GWT
	.create(GetDataService.class);
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		
		final Button sendButton = new Button("Send");
		final TextBox nameField = new TextBox();
		final Label ibatisField=new Label();
		nameField.setText("ERROR");
		final Label errorLabel = new Label();

		gridTable = new SetGrid();
			
		// We can add style names to widgets
		//sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		//RootPanel.get("nameFieldContainer").add(nameField);
		//RootPanel.get("sendButtonContainer").add(sendButton);
		//RootPanel.get("errorLabelContainer").add(errorLabel);
		//RootPanel.get("ibatisFieldContainer").add(ibatisField);

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.selectAll();

		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Please insert connection details");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		//closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		final Label userLabel=new Label("User");
		final Label pswLabel=new Label("Password");
		final Button connectButton = new Button("Connect");
		final TextBox userField = new TextBox();
		final PasswordTextBox pswField = new PasswordTextBox();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.add(userLabel);
		dialogVPanel.add(userField);
		dialogVPanel.add(pswLabel);
		dialogVPanel.add(pswField);
		//connectButton.getElement().setId("connectButton");
		dialogVPanel.add(connectButton);
		//dialogVPanel.addStyleName("dialogVPanel");
		//dialogVPanel.add(serverResponseLabel);
		
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogVPanel.add(connectButton);
		dialogBox.setWidget(dialogVPanel);
		dialogBox.center();
		closeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});

		// Set the contents of the Widget
		dialogBox.setWidget(dialogVPanel);

		connectButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				final String userToServer = userField.getText();
				final String pswToServer = pswField.getText();
			
				greetingService.greetServer(userToServer, pswToServer, 
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox
										.setText("Failure" + userToServer + pswToServer);
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								// closeButton.setFocus(true);
							}
							public void onSuccess(String result) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel
										.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(result);
								dialogBox.hide();
								// closeButton.setFocus(true);
							}						
		});
			}
		});
		
		VerticalPanel vPanel = new VerticalPanel();
		vPanel.setWidth("100%");
		vPanel.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
		
		vPanel.add(gridTable);
		RootPanel.get().add(vPanel);
		
		getDataService.getData(new AsyncCallback<List<Contact>>() {
							public void onFailure(Throwable caught) {
								RootPanel.get("nameFieldContainer").add(nameField);
							}
							public void onSuccess(List<Contact> contacts) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel
										.removeStyleName("serverResponseLabelError");
								 gridTable.addGridData(contacts);
								dialogBox.hide();
								// closeButton.setFocus(true);
							}						
		});
		
}
}
